//
//  BottomBar.m
//  Dolphin
//
//  Created by Handymood on 11-6-7.
//  Copyright 2011 __Hanydmood__. All rights reserved.
//

#import "BottomBar.h"
#import "UIImageLayer.h"

@implementation BottomBar

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {

    }
    return self;
}

-(void) initWithIndexInfo:(IndexPageInfo *) indexPageInfo
{
    slot = FALSE;
    CGFloat index_page_width = self.frame.size.width;
    CGFloat index_page_height = indexPageInfo.unit_height;
    
    indexPage = [[IndexPage alloc] initWithFrame:CGRectMake(0,index_page_height,index_page_width,index_page_height)];
    [indexPage initWithInfoObject:indexPageInfo columnNum:[indexPageInfo.unitArr count] rowNum:1];
    
    [self addSubview:indexPage];
    
    newTopBg = [[UIImageLayer alloc] initWithFrame:CGRectMake(0, index_page_height-43, 768, 43)];        
    [newTopBg initImage:@"xian_bottom.png"
        useOriginalSize:TRUE
              ratioMode:1
              layerInfo:Nil];
    [self addSubview:newTopBg];
    
    preBtn = [ImageUIButton buttonWithType:UIButtonTypeCustom];
    [preBtn addButtonWithImageLink:@"pre_btn.png" downStatusImgLink:@"pre_btn.png"];
    preBtn.button_id = 1;
    [preBtn setXY:300 offy:88];
    [preBtn addTarget:self action:@selector(mouseDownProc:) forControlEvents:UIControlEventTouchDown];
    [self addSubview:preBtn];
    
    nextBtn = [ImageUIButton buttonWithType:UIButtonTypeCustom];
    [nextBtn addButtonWithImageLink:@"next_btn.png" downStatusImgLink:@"next_btn.png"];
    nextBtn.button_id = 2;
    [nextBtn setXY:430 offy:88];
    [nextBtn addTarget:self action:@selector(mouseDownProc:) forControlEvents:UIControlEventTouchDown];
    [self addSubview:nextBtn];
    
}

-(void) mouseDownProc:(id)sender
{
    ImageUIButton *tempBtn = sender;
    
    if(tempBtn.button_id == 1)
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"pre_page_trigger" object:self];
    }
    else if(tempBtn.button_id == 2)
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"next_page_trigger" object:self];
    }
}

-(void) visibleExchange
{
    [UIView transitionWithView:self duration:0.35f 
                                  options:UIViewAnimationCurveEaseOut  
                             animations:
                           ^{ 
                               slot = !slot;
                               if (slot) 
                               {
                                   indexPage.transform = CGAffineTransformMakeTranslation(0,-indexPage.frame.size.height);
                                   
                                   newTopBg.transform = CGAffineTransformMakeTranslation(0,indexPage.frame.size.height);
                                   preBtn.transform = CGAffineTransformMakeTranslation(0,indexPage.frame.size.height);
                                   nextBtn.transform = CGAffineTransformMakeTranslation(0,indexPage.frame.size.height);
                               }
                               else
                               {
                                   indexPage.transform = CGAffineTransformMakeTranslation(0,0);
                                   
                                   newTopBg.transform = CGAffineTransformMakeTranslation(0,0);
                                   preBtn.transform = CGAffineTransformMakeTranslation(0,0);
                                   nextBtn.transform = CGAffineTransformMakeTranslation(0,0);
                               }
                               
                              }
                            completion:NULL];
    /*
    indexPage.hidden = !indexPage.hidden;
    newTopBg.hidden = !indexPage.hidden;
    preBtn.hidden = !indexPage.hidden;
    nextBtn.hidden = !indexPage.hidden;
     */
}

//the response for index page unit mouse down
-(void) indexDownProc:(id)sender
{
    ImageUIButton *tempBtn = sender;

    [[NSNotificationCenter defaultCenter] postNotificationName:@"page_id_trigger" object:tempBtn];    
}

-(void) layoutAdjustWithOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    CGRect screen_rect = [[UIScreen mainScreen] bounds];
    CGFloat screen_width = screen_rect.size.width;
    
    if(interfaceOrientation == UIInterfaceOrientationPortrait || 
       interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        screen_width = screen_rect.size.width;
        if(indexPage != Nil)
        {
            //indexPage.frame = CGRectMake(0, 0, screen_width, indexPage.frame.size.height);    
        }
    }
    else if(interfaceOrientation == UIInterfaceOrientationLandscapeRight || 
            interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
    {
        screen_width = screen_rect.size.height;
        if (indexPage != Nil) 
        {
            //indexPage.frame = CGRectMake(0, 0, screen_width, indexPage.frame.size.height);    
        }
    }
}

-(void) reset
{
    if (indexPage != Nil) 
    {
        for (int i = 0; i<[[indexPage subviews] count]; i++) 
        {
            [[[indexPage subviews] objectAtIndex:i]  removeTarget:self action:@selector(indexDownProc:)]; 
        }

        [indexPage reset];
        [indexPage removeFromSuperview];
        [indexPage release];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)dealloc
{
    [super dealloc];
}

@end
